package me.gacl.encrypt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Base64;
import java.util.Base64.*;

//import sun.misc.BASE64Decoder;
//import sun.misc.BASE64Encoder;

/*
    public static String encryptBASE64(byte[] data) {
        // BASE64Encoder encoder = new BASE64Encoder();
        // String encode = encoder.encode(data);
        // 从JKD 9开始rt.jar包已废除，从JDK 1.8开始使用java.util.Base64.Encoder
        Encoder encoder = Base64.getEncoder();
        String encode = encoder.encodeToString(data);
        return encode;
    }

    public static byte[] decryptBASE64(String data) throws Exception {
        // BASE64Decoder decoder = new BASE64Decoder();
        // byte[] buffer = decoder.decodeBuffer(data);
        // 从JKD 9开始rt.jar包已废除，从JDK 1.8开始使用java.util.Base64.Decoder
        Decoder decoder = Base64.getDecoder();
        byte[] buffer = decoder.decode(data);
        return buffer;
    }
 */

public class Base64Encrypt {

    public static void main(String args[]) throws IOException{
        /*System.out.print("请输入用户名:");
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String userName = in.readLine();
        System.out.print("请输入密码:");
        String password = in.readLine();
        BASE64Encoder encoder = new BASE64Encoder();
        System.out.println("编码后的用户名为:" + encoder.encode(userName.getBytes()));
        System.out.println("编码后的密码为:" + encoder.encode(password.getBytes()));*/
        
    	 //-z 从JKD 9开始rt.jar包已废除，从JDK 1.8开始使用java.util.Base64.Encoder
    		Decoder decoder = Base64.getDecoder(); ;
        //邮件主题的Base64编码
        String emailSubject = "6YKu5Lu257yW56CBCg==";//UTF-8编码 测试
        //邮件文本内容的Base64编码
        String emailPlainContent = "vPK1pbXE08q8/reiy82y4srUo6E=";//GBK编码 测试
        //带html标签和邮件内容的Base64编码
        String emailHtmlContent = "PFA+vPK1pbXE08q8/reiy82y4srUo6E8L1A+";//GBK编码 测试
        //将使用Base64编码过后的文本内容再使用Base64来解码
        emailSubject = new String(decoder.decode(emailSubject),"UTF-8");
        emailPlainContent = new String(decoder.decode(emailPlainContent),"GBK");
        emailHtmlContent = new String(decoder.decode(emailHtmlContent),"GBK");
        System.out.println("邮件标题："+emailSubject);
        System.out.println("邮件内容："+emailPlainContent);
        System.out.println("带html标签的邮件内容："+emailHtmlContent);
    }
}


